/**
 * 浏览页面顶部搜索框展开收回控制
 */
function toggleSearchFormShow()
{
    let $ = layui.$;
    let items = $('.top-search-from .layui-form-item');
    if (items.length <= 2) {
        if (items.length <= 1) $('.top-search-from').parent().parent().remove();
        return;
    }
    let btns = $('.top-search-from .toggle-btn a');
    let toggle = toggleSearchFormShow;
    if (typeof toggle.hide === 'undefined') {
        btns.on('click', function () {
            toggle();
        });
    }
    let countPerRow = parseInt($('.top-search-from').width()/$('.layui-form-item').width());
    if (items.length <= countPerRow) {
        return;
    }
    btns.removeClass('layui-hide');
    toggle.hide = !toggle.hide;
    if (toggle.hide) {
        for (let i = countPerRow - 1; i < items.length - 1; i++) {
            $(items[i]).hide();
        }
        return $('.top-search-from .toggle-btn a:last').addClass('layui-hide');
    }
    items.show();
    $('.top-search-from .toggle-btn a:first').addClass('layui-hide');
}

var $ = layui.$

layui.$(function () {
    toggleSearchFormShow();
});


/**
 * 传入文本内容，对其进行复制
 */
//复制指定内容
function copy_text(str){
    var html = document.createElement('div');
    html.innerText = str;
    html.id = 'copy-text';
    document.body.appendChild(html);
    //复制指定标签内容
    var my_copy = (element) => {
        /*对标签id中文本进行全选*/
        var text = document.getElementById(element);
        if (document.body.createTextRange) {
            var range = document.body.createTextRange();
            range.moveToElementText(text);
            range.select();
        } else if (window.getSelection) {
            var selection = window.getSelection();
            var range = document.createRange();
            range.selectNodeContents(text);
            selection.removeAllRanges();
            selection.addRange(range);
        } else {
            alert("none");
            return false;
        }
        // 复制选中的内容
        if (document.execCommand('copy', false, null)) {
            return 1;
        } else {
            return 0;
        }
    }
    var res = my_copy(html.id);
    html.remove();
    return res;
}


// 创建一个上传选框
function importFileView(apiUrl,cb){
    var input = document.createElement('input');
    input.type= 'file'
    input.name='excel'
    input.id = 'excelDom'
    input.style = "position: absolute;left: -400px;top: -400px;";
    input.onchange = function(){
        var file = this.files[0];
        importFile(file,apiUrl,cb)
    };
    document.body.appendChild(input);
    input.click();// 触发上传效果
}

// 上传并导入文件
function importFile(files,apiUrl,cb){
    let $ = layui.$;
    let layer = layui.layer;
    var fromObj=new FormData();
    //循环像from表单对象里面添加 图片数据
    fromObj.append('excel',files);
    var loadi = layer.load();
    $.ajax({
        url:'/admin/app/data-haiyun/excelUpload',
        type:'POST',
        data:fromObj,
        contentType:false,//如果是传图片则这俩项需要为false
        processData:false,
        success:function(e){
            if(e.code==0){
                $.post(apiUrl,e.data,function(res){
                    if(res.code==101){
                        layer.close(loadi)
                        layer.confirm(res.msg,{
                            title:'温馨提示',
                            btn:['覆盖导入','不覆盖导入','暂不导入'],
                            btn3:function(index,layero){
                                layer.close(index)
                            }
                        },function(index,layero){
                            var loadi = layer.load();
                            e.data.cover = 1;// 1 覆盖  2 不覆盖
                            $.post(apiUrl,e.data,function(res){
                                layer.close(loadi)
                                cb(res)
                            })
                        },function(index,layero){
                            var loadi = layer.load();
                            e.data.cover = 2;// 1 覆盖  2 不覆盖
                            $.post(apiUrl,e.data,function(res){
                                layer.close(loadi)
                                cb(res)})
                        });
                    }else{
                        layer.close(loadi)
                        cb(res);
                    }
                })
            }else{
                layer.close(loadi)
                layer.msg(e.msg,{icon:5});
            }
            // 移除上传框
            document.getElementById('excelDom').remove();
        },
    })
}


function getRandomStr(length) {
    length = length || 10;
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    for (var i = 0; i < length; i++) {
        var randomIndex = Math.floor(Math.random() * characters.length);
        result += characters.charAt(randomIndex);
    }
    return result;
}

// 绑定页面标签  打开tab
function bindDataUrl(){
    $("body").on("click", "[data-url]", function() {
        let _layui = parent.layui.tab || parent.parent.layui.tab
        _layui.addTabOnlyByElem("content", {
            id: $(this).attr("data-id") || $(this).attr("data-title"),
            title: $(this).attr("data-title"),
            url: $(this).attr("data-url"),
            close: true
        },0)
    })
}
bindDataUrl();




